package com.campus.controller;

import com.campus.model.Activity;
import com.campus.service.ActivityService;
import com.campus.web.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/homework/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    // 创建活动
    @PostMapping("/create")
    public ResponseResult<String> createActivity(@RequestBody Activity activity) {
        int id = activityService.createActivity(activity);
        if (id > 0) {
            return ResponseResult.ok("Activity created with ID: " + id);
        } else {
            return ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to create activity.");
        }
    }

    // 更新活动
    @PutMapping("/update")
    public ResponseResult<String> updateActivity(@RequestBody Activity activity) {
        boolean success = activityService.updateActivity(activity);
        if (success) {
            return ResponseResult.ok("Activity updated.");
        } else {
            return ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to update.");
        }
    }

    // 发布活动
    @PostMapping("/publish/{id}")
    public ResponseResult<String> publishActivity(@PathVariable int id) {
        boolean success = activityService.publishActivity(id);
        if (success) {
            return ResponseResult.ok("Activity published.");
        } else {
            return ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to publish.");
        }
    }

    // 查询活动
    @GetMapping("/query")
    public ResponseResult<List<Activity>> queryActivities(
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String name,
            @RequestParam(required = false, defaultValue = "created_time") String orderBy) {
        List<Activity> activities = activityService.queryActivities(status, name, orderBy);
        if (activities != null && !activities.isEmpty()) {
            return ResponseResult.ok(activities, "Activities fetched successfully.");
        } else {
            return ResponseResult.error(HttpStatus.NOT_FOUND, "No activities found.");
        }
    }

    // 查询所有活动
    @GetMapping("/list")
    public ResponseResult<List<Activity>> listActivities() {
        List<Activity> activities = activityService.listAllActivities();
        if (activities != null && !activities.isEmpty()) {
            return ResponseResult.ok(activities, "All activities fetched successfully.");
        } else {
            return ResponseResult.error(HttpStatus.NOT_FOUND, "No activities found.");
        }
    }

    // 删除活动
    @DeleteMapping("/delete/{id}")
    public ResponseResult<String> deleteActivity(@PathVariable int id) {
        boolean success = activityService.deleteActivity(id);
        if (success) {
            return ResponseResult.ok("Activity deleted Successfully.");
        } else {
            return ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to delete activity.");
        }
    }

}
